package cn.com.jtang.wedding.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.com.jtang.wedding.model.Charge;
import cn.com.jtang.wedding.service.ChargeManager;


import com.opensymphony.xwork2.Preparable;

public class ChargeAction extends BaseAction implements Preparable {
	
	private ChargeManager chargeManager;
	private Charge charge;
	private String chargeName;
	private String price;
	private String description;
	
	private long cid;
	private String id;
	private String[] chb;
	
	private List<Charge> charges;
	Map<String, Object> map = new HashMap<String, Object>();

	
	public void prepare() throws Exception {
		// TODO Auto-generated method stub

	}
	
	public String insert(){
		if(chargeName!=null&&price!=null&&description!=null)	{
			charge = new Charge();
			charge.setChargeName(chargeName);
			charge.setPrice(price);
			charge.setDescription(description);
			chargeManager.insert(charge);
			return SUCCESS;
		}else{
			return ERROR;
		}
	}
	/**
	 * 多条记录删除
	 */
	public String delete(){
		charge = new Charge();
		if(chb!=null)
		{
			for(int i=0;i<chb.length;i++){
				cid = new Long(chb[i]).longValue();
				chargeManager.deleteById(cid);
			}
		}
		return SUCCESS;
	}
	/**
	 * 
	 * 单条记录删除
	 */
	public String deleteforSingle(){
		charge = new Charge();
		if(id!=null){
			cid = new Long(id).longValue();
			chargeManager.deleteById(cid);
		}
		return SUCCESS;		
	}
	
	public String update(){
		charge = new Charge();
		cid = new Long(id).longValue();
		charge.setId(cid);
		charge.setChargeName(chargeName);
		charge.setPrice(price);
		charge.setDescription(description);
		chargeManager.updateById(cid, charge);
		return SUCCESS;
	}
	/**
	 * 获取当前记录信息
	 */
	public String getChargeforUpdate(){
		charge = new Charge();
		cid = new Long(id).longValue();
		charge = chargeManager.queryById(cid);
		chargeName = charge.getChargeName();
		price = charge.getPrice();
		description = charge.getDescription();
		return SUCCESS;	
	}
	
	/**
	 * 获取所有记录列表
	 */
	public String getAll(){		
		pageNum = (pageNum) <= 0 ? 1 : pageNum;
		//map.put("adminUserId", getCurrentAdminUser().getId());
		//int count = chargeManager.getCount(map);
		int count = chargeManager.getCount(null);
		charges = new ArrayList<Charge>();
		pageCount = (int) Math.ceil((double) count / (double) pageSize);
		pageCount=pageCount==0?1:pageCount;
		pageNum = pageNum > pageCount ? pageCount : pageNum;
		charges = chargeManager.queryAll(null, pageSize * (pageNum - 1),
				pageSize * pageNum);
		return SUCCESS;
	}

	public ChargeManager getChargeManager() {
		return chargeManager;
	}

	public void setChargeManager(ChargeManager chargeManager) {
		this.chargeManager = chargeManager;
	}

	public Charge getCharge() {
		return charge;
	}

	public void setCharge(Charge charge) {
		this.charge = charge;
	}

	public String getChargeName() {
		return chargeName;
	}

	public void setChargeName(String chargeName) {
		this.chargeName = chargeName;
	}

	public String getPrice() {
		return price;
	}

	public void setPrice(String price) {
		this.price = price;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String[] getChb() {
		return chb;
	}

	public void setChb(String[] chb) {
		this.chb = chb;
	}

	public List<Charge> getCharges() {
		return charges;
	}

	public void setCharges(List<Charge> charges) {
		this.charges = charges;
	}

	public Map<String, Object> getMap() {
		return map;
	}

	public void setMap(Map<String, Object> map) {
		this.map = map;
	}
}
